﻿Imports ATS.UI.Process
Imports ATS.Business.Entities
Imports System.ComponentModel

Public Class ReportSettings
    Public WithEvents oWindowList As OpenedWindowList = OpenedWindowList.GetList()
    Private WithEvents oWindow As OpenedWindowClass
    Private _companyDetail As CompanyDetail = Nothing
    Dim v_isdirty As Boolean = False
    Private _upc As SubmitterUserProcess = Nothing

#Region "Control Events"

#End Region

#Region "Window Events"
    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        _upc = New SubmitterUserProcess()
    End Sub

    Private Sub ReportSettings_Closed(sender As Object, e As System.EventArgs) Handles Me.Closed
        Dim ied As IEnumerable(Of OpenedWindowClass) = From d In oWindowList Where d.WindowID = Me.Name
        oWindowList.Remove(ied.First)
        If Application.Current.Windows.Count = 1 Then
            Application.Current.Windows(0).Activate()
        End If
    End Sub

    Private Sub ReportSettings_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
        If v_isdirty Then
            If MessageBox.Show("There are unsaved entries. Do you really want to exit?", "Exit", MessageBoxButton.YesNo, MessageBoxImage.Warning) = MessageBoxResult.No Then
                e.Cancel = True
            End If
            SetWindowProperties(False)
        End If

    End Sub

    Private Sub ReportSettings_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        LoadDefaults()
        HookupChangeEventHandler(_companyDetail)
        _companyDetail.ResetDirtyBit()
    End Sub

#End Region

#Region "Support Procedures"
    Private Sub SetWindowProperties(dirty As Boolean)
        If dirty Then
            Me.Title &= IIf(Not Me.Title.ToUpper.Contains("UNSAVED"), " (unsaved)", "")
        Else
            Me.Title = IIf(Title.ToUpper.Contains("UNSAVED"), Me.Title.Substring(0, Me.Title.Length - 10), Me.Title)
        End If
        v_isdirty = dirty

        For itemIndex = 0 To oWindowList.Count - 1
            If oWindowList(itemIndex).WindowID = Me.Name Then
                oWindowList(itemIndex).WindowName = Me.Title
                oWindowList(itemIndex).WindowIsDirty = dirty
            End If
        Next
    End Sub

    Private Sub LoadDefaults()
        textboxReportFilesLocation.Text = My.Settings.reportLocation
        CheckBoxShowAmount.IsChecked = My.Settings.reportHeaderShowAmounts
        _companyDetail = _upc.ReadCompanyDetail()
        Me.DataContext = _companyDetail
        SetWindowProperties(False)
    End Sub

    Private Sub HookupChangeEventHandler(ByVal _companyDetail As CompanyDetail)
        ' Add a PropertyChanged event handler for 
        ' the specified Customer instance:
        AddHandler _companyDetail.PropertyChanged, _
          AddressOf HandlePropertyChanged
    End Sub

    Private Sub HandlePropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)
        If _companyDetail.IsDirty Then
            SetWindowProperties(_companyDetail.IsDirty)
            v_isdirty = _companyDetail.IsDirty
        End If
    End Sub


#End Region

#Region "Control Events"
    Private Sub buttonSave_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles buttonSave.Click
        If v_isdirty Then
            Try
                My.Settings.reportLocation = textboxReportFilesLocation.Text
                My.Settings.reportHeaderShowAmounts = CheckBoxShowAmount.IsChecked
                My.Settings.Save()
                _upc.UpdateCompanyDetail(_companyDetail)
                MessageBox.Show("Data has been saved successfully.", "ATS", MessageBoxButton.OK, MessageBoxImage.Information)
                _companyDetail.ResetDirtyBit()
                SetWindowProperties(_companyDetail.IsDirty)
            Catch ex As Exception
                MessageBox.Show("Nothing saved, values were not changed.", "ATS", MessageBoxButton.OK, MessageBoxImage.Information)
            End Try
        End If
    End Sub

    Private Sub CheckBoxShowAmount_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles CheckBoxShowAmount.Checked, CheckBoxShowAmount.Unchecked
        SetWindowProperties(True)
    End Sub

    Private Sub buttonBrowseReport_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles buttonBrowseReport.Click
        Dim dlg As New System.Windows.Forms.FolderBrowserDialog
        dlg.SelectedPath = textboxReportFilesLocation.Text
        If dlg.ShowDialog() = Forms.DialogResult.OK Then
            textboxReportFilesLocation.Text = dlg.SelectedPath
            SetWindowProperties(True)
        Else
            textboxReportFilesLocation.Text = My.Settings.reportLocation
        End If
    End Sub

    Private Sub buttonRefresh_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles buttonRefresh.Click
        If v_isdirty = True Then
            If MessageBox.Show("There are unsaved entries. Do you really want refresh the data?", "Refresh", MessageBoxButton.YesNo, MessageBoxImage.Warning) = MessageBoxResult.Yes Then
                LoadDefaults()
            End If
        End If
    End Sub
#End Region

    
   
   
End Class
